package org.lq.er.view.exam_manage;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.lq.er.entity.Exam_position;
import org.lq.er.service.Exam_positionService;
import org.lq.er.service.impl.Exam_positionServiceImpl;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * 
 * @author 韩伟伟
 *
 * @package org.lq.er.view.exam_manage
 */
public class V_examPosition_add extends JFrame {

	private JPanel contentPane;
	private JTextField ep_position;
	private JTextField ep_conut;
	private Exam_positionService ps = new Exam_positionServiceImpl();
	private JTextField ep_no;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_examPosition_add frame = new V_examPosition_add();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_examPosition_add() {
		setTitle("添加考场数据");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 315, 258);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		
		JLabel lbl_cardId = new JLabel("考场位置:");
		lbl_cardId.setBounds(54, 62, 54, 15);
		
		JLabel lbl_name = new JLabel("考场人数:");
		lbl_name.setBounds(54, 105, 54, 15);
		
		ep_position = new JTextField();
		ep_position.setBounds(118, 59, 136, 21);
		ep_position.setColumns(10);
		
		ep_conut = new JTextField();
		ep_conut.setBounds(118, 102, 136, 21);
		ep_conut.setColumns(10);
		panel.setLayout(null);
		panel.add(lbl_name);
		panel.add(lbl_cardId);
		panel.add(ep_conut);
		panel.add(ep_position);
		
		JButton btn_save = new JButton("保存");
		btn_save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 创建考场对象
				Exam_position exp = new Exam_position();
				exp.setId(castInt(ep_no.getText()));
				exp.setPosition(ep_position.getText());
				exp.setMax_count(castInt(ep_conut.getText()));
				
				// 询问是否添加
				int num = JOptionPane.showConfirmDialog(V_examPosition_add.this, "是否添加?", "提示", JOptionPane.INFORMATION_MESSAGE);
				if (num == 0) {
					// 添加操作
					Exam_position ep = ps.getById(exp.getId());
					if (ep != null) {
						JOptionPane.showMessageDialog(V_examPosition_add.this, "该考场信息已存在!", "错误", JOptionPane.ERROR_MESSAGE);							
					} else {							
						if (ps.save(exp)) {
							JOptionPane.showMessageDialog(V_examPosition_add.this, "添加成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
						} else {
							JOptionPane.showMessageDialog(V_examPosition_add.this, "添加失败!", "错误", JOptionPane.ERROR_MESSAGE);
						}
					}
				}
			}
		});
		btn_save.setBounds(94, 156, 93, 23);
		panel.add(btn_save);
		
		JLabel lblNewLabel = new JLabel("考场序号:");
		lblNewLabel.setBounds(54, 25, 60, 15);
		panel.add(lblNewLabel);
		
		ep_no = new JTextField();
		ep_no.setBounds(118, 22, 136, 21);
		panel.add(ep_no);
		ep_no.setColumns(10);
	}
	
	
	
	public static String castString(Object obj) {
		return castString(obj, "");
	}
	public static String castString(Object obj,String defaultValue) {
		return obj != null ? String.valueOf(obj) : defaultValue;
	}
	public static int castInt(Object obj,int defaultValue) {
		int value = defaultValue;
		if(obj!=null) {
			String strVal = castString(obj);
			if(!"".equals(strVal)) {
				try {
					value = Integer.valueOf(strVal);
				} catch (NumberFormatException e) {
					value = defaultValue;
					e.printStackTrace();
				}
			}
		}
		return value;
	}
	public static int castInt(Object obj) {
		return castInt(obj, 0);
	}
}
